#!/usr/bin/perl -w

#####################################################################
# TankStats Modules
#####################################################################
use FindBin qw($Bin);
use lib "$Bin/..";
use config;
use layout;

#####################################################################
# Program Modules
#####################################################################
# CGI Modules
use CGI;
use CGI::Carp;
use CGI::Pretty;

# Database Modules
use DBI;

# Data::Dumper for debugging
#use Data::Dumper;

# POSIX Stuff...
use POSIX;

#####################################################################
# Program Start
#####################################################################
my $cgi = new CGI;

my $pagetitle = &read_config_option("tank_name") . " Index Page";

&ts_pagestart($pagetitle);

# Save the config variables if the user hit the "Save Config" Button
if (defined($cgi->param('save_config'))) {
    my %params = $cgi->Vars;
    &save_config(
        $cgi->param('tank_name'), $cgi->param('png_opts'), $cgi->param('controller_type'), $cgi->param('controller_url'),
        $cgi->param('stats_message'));
}

# Start the forms
print $cgi->start_form;
print $cgi->start_table({ -align => 'center' });

# The Tank Name
print $cgi->Tr([
        $cgi->th({ -class => "right" }, ["Tank Name"])
          . $cgi->td(
            { -class => "left" }, [ $cgi->textfield(-name => "tank_name", -default => (&read_config_option("tank_name") || ""), -size => 50) ]) ]);

# The Graph Options
print $cgi->Tr([
            $cgi->th({ -class => "right" }, ["Graph Options"])
          . $cgi->td({ -class => "left" }, [ $cgi->textfield(-name => "png_opts", -default => (&read_config_option("png_opts") || ""), -size => 50) ])
    ]);

# Tank Controller Type
print $cgi->Tr([
        $cgi->th({ -class => "right" }, ["Tank Controller"])
          . $cgi->td(
            { -class => "left" }, [
                $cgi->popup_menu(
                    -name    => "controller_type",
                    -values  => [@controller_types],
                    -default => (&read_config_option("controller_type") || "")) ]) ]);

# Controller URL
print $cgi->Tr([
        $cgi->th({ -class => "right" }, ["Controller URL"])
          . $cgi->td(
            { -class => "left" },
            [ $cgi->textfield(-name => "controller_url", -default => (&read_config_option("controller_url") || ""), -size => 50) ]) ]);

# Tank Stats Screen Text
print $cgi->Tr([
        $cgi->th({ -class => "right" }, ["Stats Screen Message"])
          . $cgi->td(
            { -class => "left" },
            [ $cgi->textarea(-name => "stats_message", -default => (&read_config_option("stats_message") || ""), -rows => "5", -cols => "100") ]) ]);

# The save/reset buttons
print $cgi->Tr([ $cgi->td({ -colspan => "2" }, $cgi->submit(-name => "save_config", -label => "Save Configuration") . " | " . $cgi->reset()) ]);
print $cgi->end_table;
print $cgi->end_form;
print $cgi->p($cgi->center("Return to the " . $cgi->a({ -href => "./" }, &read_config_option("tank_name") . " Configuration Index") . " Page."));
print $cgi->end_html;
print "\n";
exit 0;

#####################################################################
# Program Subroutines
#####################################################################
# Write/Upoate the test to the database
sub save_config {
    my $tank_name       = shift @_;
    my $png_opt         = shift @_;
    my $controller_type = shift @_;
    my $controller_url  = shift @_;
    my $stats_message   = shift @_;

    # Open the Database
    my $dbh = DBI->connect("dbi:SQLite:dbname=$database_file", "", "");

    $dbh->do("INSERT OR REPLACE INTO config (config_option, config_value) VALUES (?, ?)", undef, "tank_name",       $tank_name);
    $dbh->do("INSERT OR REPLACE INTO config (config_option, config_value) VALUES (?, ?)", undef, "png_opts",        $png_opt);
    $dbh->do("INSERT OR REPLACE INTO config (config_option, config_value) VALUES (?, ?)", undef, "controller_type", $controller_type);
    $dbh->do("INSERT OR REPLACE INTO config (config_option, config_value) VALUES (?, ?)", undef, "controller_url",  $controller_url);
    $dbh->do("INSERT OR REPLACE INTO config (config_option, config_value) VALUES (?, ?)", undef, "stats_message",   $stats_message);

    # Close the database
    $dbh->disconnect;

    print $cgi->comment("Entry Added/Updated"), "\n";
    $cgi->delete_all();
    return;
}

